var basePage = require('../../../common/BasePage');
Page({
    behaviors: [basePage],
    data: {
        page: 1,
        totalPages: 0,
        loading: false
    },
    onShow: function() {
        
    },
	onLoad: function(options) {
	
		this.GetCouponList()
	},
	toCat(){
		  wx.switchTab({
		  	url: "/pages/cat/Index",
		  })
	},
    GetCouponList() {
        this.setData({ loading: true })
        let that = this
        this.request({
            url: this.data.host + '/home/coupon/index',
            data: {
                page: this.data.page
            }
        }, function(data) {
            let list = data.data
            // list = list.filter(item => {
            //     return item.status == 3
            // })
			let couponList=[]
			if(that.data.page==1){
				couponList=list;
			}else{
				couponList = that.data.couponList || []
				couponList = [...couponList, ...list]
			}
            
            that.setData({ couponList: couponList, loading: false })
        })
    },
    GetCoupon(e) {
        const { id } = e.target.dataset
        let that = this
        this.request({
            url: this.data.host + '/home/coupon/sign',
            method: 'POST',
            data: {
                coupon_id: id
            },
            loadingTip: '领取中'
        }, function() {
            let { couponList } = that.data
            const index = couponList.findIndex(item => {
                return item.coupon_id == id
            })
            couponList[index].status = 0
            that.setData({ couponList })
            wx.showToast({
                title: '领取成功',
                icon: 'success'
            })
        })
    },
    login_reload() {
        this.GetCouponList()
    },
    onReachBottom: function() {
        if (this.data.page >= this.data.totalPages){
			console.log("****")
			return
		} 
        let { page } = this.data
        page++
        this.setData({ page })
        this.GetCouponList()
    },
	// onShareAppMessage(res) {
		
	// },
})